﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using FmcWebsite.Core;

namespace FmcWebsite.Web
{
    public class WebArticle
    {
        private Article article;        
        private HttpSessionState session;

        private string GetLanguage()
        {
            string lang = (string)session["language"];
            return (lang == null) ? "EN" : lang;            
        }

        private WebArticle(Article article, HttpSessionState session)
        {
            this.article = article;
            this.session = session;
        }
        public int Id
        {
            get { return article.Id; }
        }

        public string Title
        {
            get
            {
                return Multilang.Extract(article.Title, GetLanguage());
            }
        }

        public string Content
        {
            get
            {
                var content = Multilang.Extract(article.Content, GetLanguage());
                return HttpUtility.HtmlDecode(Rich2Html.Convert(content));
            }
        }

        public DateTime InsertDate
        {
            get { return article.InsertDate; }
        }

        public IEnumerable<AttachmentIMAGE> Images
        {
            get
            {
                return article.Images;
            }
        }

        public IEnumerable<WebAttachmentImage> WebImages
        {
            get
            {
                foreach (AttachmentIMAGE img in article.Images)
                    yield return new WebAttachmentImage(img, this.session);
            }
        }

        public AttachmentAUDIO Audio
        {
            get 
            {
                return article.Audio;
            }
        }

        /// <summary>
        /// Returns link to display the article page
        /// </summary>
        /// <returns></returns>
        public string GetLink()
        {
            return string.Format("<a href=\"{0}?id={1}\">{2}</a>", DefaultArticlePage, Id, Title);
        }

        static public string DefaultArticlePage = "\\Article.aspx";

        /// <summary>
        /// Get the web article from articles. This will filter only the 
        /// articles that have property Publish True.
        /// </summary>
        /// <param name="articles"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        static private IEnumerable<WebArticle> FromArticle(IEnumerable<Article> articles, HttpSessionState session)
        {
            foreach (Article a in articles.Where(x => x.Publish))
            {                
                yield return new WebArticle(a, session);
            }
        }

        static public WebArticle ById(int art_id, HttpSessionState session)
        {
            return new WebArticle(Article.ById(art_id), session);
        }

        static public IEnumerable<WebArticle> GetAll(HttpSessionState session)
        {
            return FromArticle(Article.GetAll(), session);
        }

        static public IEnumerable<WebArticle> ByCategory(WebCategory category, bool incl_children)
        {
            return FromArticle(Article.ByCategory(category.Category, incl_children), category.Session);
        }

        static public IEnumerable<WebArticle> ByCategory(WebCategory category)
        {
            return ByCategory(category, false);
        }

        static public IEnumerable<WebArticle> ByCategory(string category_code, HttpSessionState session)
        {
            return ByCategory(WebCategory.ByCode(category_code, session), false);
        }

        static public IEnumerable<WebArticle> ByCategory(string category_code, HttpSessionState session, bool incl_children)
        {
            return ByCategory(WebCategory.ByCode(category_code, session), incl_children);
        }

        static public WebArticle ByCode(string code, HttpSessionState session)
        {
            return new WebArticle(Article.ByCode(code), session);
        }

    }
}
